//
//  NSObject+Json.m
//  智能备考学习
//
//  Created by 盛超 on 2018/8/15.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "NSObject+Json.h"

@implementation NSObject (Json)

+(NSString *)objectToJson:(id)objc {
    
    if ([objc isKindOfClass:[NSDictionary class]]) {
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objc options:NSJSONWritingPrettyPrinted error:&error];
        
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    } else if ([objc isKindOfClass:[NSArray class]]) {
        
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:objc options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        return jsonTemp;
    }
    
    return @"";
}

@end
